﻿<?php
	include_once '../action/checkLogin.php';
	include_once '../../public/inc/DB.php';
	$db = new DB('jusmor_resource');
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>资源列表</title>
	<link type="text/css" rel="stylesheet" href="/public/css/bootstrap.min.css" />
	<link rel="stylesheet" type="text/css" href="/public/otherPlus/uploadify/uploadify.css">
    <script type="text/javascript" src="/public/js/jquery-1.8.3.min.js"></script>
</head>
<body>
	<div class="container col-lg-12 ">
        <h3 style="border-bottom:3px solid #336699; padding-bottom:2px;">
			<strong>资源列表</strong>&nbsp;
			<small><button class="btn btn-primary btn-sm" data-toggle="modal" id="upload"> 资源上传</button></small>
		</h3>
		<div class="table-responsive">
            <table class="table table-bordered table-hover">
                <thead style="background:#EEEEEE;">
                    <th>ID</th>
                    <th>资源名称</th>
                    <th>来源</th>
					<th>大小</th>
					<th>上传时间</th>
					<th>操作</th>
                </thead>
                <tbody>
				<?php 
                	$fields = 'res_id, res_name, source, size, create_time';
                	$order = array('create_time'=>'desc');
                	$result = $db->fetchAll('', $fields, '', '', '', $order);
					if(! empty($result)) {
                		foreach ($result as $key=>$val) {
                ?>
				<tr>
					<td class=""><?php echo $val['res_id']; ?></td>
					<td class=""><?php echo $val['res_name']; ?></td>
					<td class=""><?php echo $val['source']; ?></td>
					<td class=""><?php echo $val['size']; ?></td>
					<td class=""><?php echo $val['create_time']; ?></td>
					<td>
						<button type="button" class="btn btn-primary btn-xs updateModel">修改</button>
						<button type="button" data-id="<?php echo $val['res_id']; ?>" class="btn btn-danger btn-xs delRes">删除</button>
					</td>
				</tr>
				<?php 
						}
					} else {
                ?>
                	<tr class="text-center"><td colspan="6">没有上传资源！</td></tr>
                <?php
					}
				?>
                </tbody>
            </table>
        </div>
	</div>
	
	<div class="modal fade" id="uploadModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
		  <div class="modal-dialog">
			<div class="modal-content">
			  <div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
				<h4 class="modal-title" id="myModalLabel">上传资源文件</h4>
			  </div>
			  <div class="modal-body" style="">
			  <div class="row text-center">
				<form class="form-horizontal" role="form" id="upload_form">
					<input type="hidden" id="res_file" name="res_file" value="" />
					<input type="hidden" id="res_size" name="res_size" value="" />
					<div class="form-group">
						<label for="inputName" class="col-sm-2 control-label">资源名称</label>
						<div class="col-sm-9">
							<input type="text" class="form-control" id="inputName" name="res_name" placeholder="资源名称" />
						</div>
					</div>
					
					<div class="form-group">
						<label for="inputSource" class="col-sm-2 control-label">来源</label>
						<div class="col-sm-9">
							<input type="text" class="form-control" id="inputSource" name="source" placeholder="来源" />
						</div>
					</div>
					
					<div class="form-group">
						<label for="file_upload" class="col-sm-2 control-label"></label>
						<div class="col-sm-9">
						<div id="queue"></div>
							<button id="file_upload" class="btn btn-primary" name="file_upload" type="file" multiple>请选择文件</button>
						</div>
					</div>
					
					<div class="form-group" id="div_res_file" style="display:none;">
						<label for="lb_res_file" class="col-sm-2 control-label"></label>
						<div class="col-sm-9">
							<label style="color:red;">已上传文件：</label><label id="lb_res_file"></label>
						</div>
					</div>
				</form>
				</div>
			  </div>
			  <div class="modal-footer">
			  	 <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
				 <button type="button" class="btn btn-primary" id="add_upload">添加</button>
			  </div>
			</div>
		  </div>
		</div>
	</div>
	<script type="text/javascript" src="/public/js/bootstrap.min.js"></script>
	<script src="/public/otherPlus/uploadify/jquery.uploadify.min.js" type="text/javascript"></script>
    <script type="text/javascript" src="/public/js/tools.js"></script>
	<script type="text/javascript" src="/public/js/res_upload.js"></script>
	
	<script type="text/javascript">
		$(function(){
			$("#upload").click(function () {
            	//初始化上传插件
            	$('#file_upload').uploadify(imgup);
            	//调用弹出层
            	$("#uploadModal").modal("show");
        	});	
			
			$("#add_upload").click(function(){
				var res_file = $.trim($("#res_file").val());
				var res_size = $.trim($("#res_size").val());
				var res_name = $.trim($("#inputName").val());
				var source = $.trim($("#inputSource").val());
				if(res_name.length <=0) {
					$("#inputName").focus();
				} else if(source.length <= 0) {
					$("#inputSource").focus();
				} else if(res_file.length <= 0 || res_size.length <=0) {
					alert("没有上传文件，请上传文件？");	
				} else {
					var _url = "../ajaxAPI/resource.php";
					var _param = {"res_file":res_file, "size":res_size, "res_name":res_name, "source":source, "action":"save_res"};
					$.post(_url, _param, function(data){
						if(data.code == 1) {
							$("#uploadModal").modal("hide");
							alert("资源上传成功！");
							refresh();	
						} else {
							alert(data.msg);	
						}
					}, "json")
				}
			})
			
			$(".delRes").click(function(){
				var _this = $(this);
				var res_id = _this.attr("data-id");
				if(res_id != "") {
					if(confirm("确认删除此资源?（资源文件将不会被删除）")) {
						var _url = "../ajaxAPI/resource.php";
						var _param = {"res_id":res_id, "action":"del_res"};
						$.post(_url, _param, function(data) {
							if(data.code == 1) {
								var _tr = _this.closest("tr");
								_tr.hide("500", function() {
									_tr.remove();
								})
							} else {
								alert(data.msg);
							}
						}, "json")
					}
				}
			})
		})
	</script>
</body>
</html>
